package chou_hong_bao;

import java.util.Random;

public class MyThread extends Thread{
    //共享数据
    //100元红包分成了3个
    private double money = 100;//100元
    private int count = 3; //红包个数
    private double MIN = 0.01;//最小红包

    @Override
    public void run() {
        //循环
        //同步代码块
        //判断，共享数据已经到末尾
        //判断，共享数据没有到末尾
        synchronized (MyThread.class){//锁对象
            if (count == 0){
                System.out.println(currentThread().getName() + "没有抢到红包");
            }
            else {
                double redPacket = 0;
                if (count == 1){
                    redPacket = money;
                }
                else {
                    //抢红包
                    Random random = new Random();
                    //100 - (3 -1 ) * 0.01
                    double bounds = money - (count - 1) * MIN;//红包金额
                    //抢红包
                    redPacket = random.nextDouble(bounds);//随机红包
                }
                money -= redPacket;
                count--;
                System.out.println(currentThread().getName() + "抢到了：" + redPacket+"元");
            }

        }
    }
}
